package ch.unisi.inf.editor;

import java.awt.Color;
import ch.unisi.inf.engine.Image;
/**
 * subclass of the ImageInfo class, returns the hue of an Image
 * 
 * @author  matthias.eberli@lu.unisi.ch
 */
public class InfoHue extends ImageInfo
{
    public InfoHue(final String name)
    {
        super(name);
    }
    
    public int getInfo(final Image image)
    {
        int hue = 0;
        final int height = image.getHeight();
        final int width = image.getWidth();
        for(int y = 0; y < height; y++) {
            for(int x = 0; x < width; x++) {
                Color pixel = image.getPixel(x, y);
                
                if(pixel.getRed() > pixel.getGreen() && pixel.getBlue() <= pixel.getGreen() ){
                    hue += (60*(pixel.getGreen() - pixel.getBlue())/(pixel.getRed() - pixel.getBlue()));
                }
                else if(pixel.getGreen() > pixel.getRed() && pixel.getBlue() <= pixel.getRed() ){
                    hue += (60*(1+(pixel.getRed() - pixel.getBlue())/(pixel.getGreen() - pixel.getBlue())));
                }
                else if(pixel.getGreen() >= pixel.getBlue() && pixel.getRed() < pixel.getBlue() ){
                    hue += (60*(2+(pixel.getBlue() - pixel.getRed())/(pixel.getGreen() - pixel.getRed())));
                }
                else if(pixel.getBlue() > pixel.getGreen() && pixel.getRed() < pixel.getGreen() ){
                    hue += (60*(3+(pixel.getGreen() - pixel.getRed())/(pixel.getBlue() - pixel.getRed())));
                }
                else if(pixel.getBlue() > pixel.getRed() && pixel.getGreen() <= pixel.getRed() ){
                    hue += (60*(4+(pixel.getRed() - pixel.getGreen())/(pixel.getBlue() - pixel.getGreen())));
                }
                else if(pixel.getRed() >= pixel.getBlue() && pixel.getGreen() < pixel.getBlue() ){
                    hue += (60*(5+(pixel.getBlue() - pixel.getGreen())/(pixel.getRed() - pixel.getGreen())));
                }
            }
        }
        hue = hue / (height * width);
        return hue;
    }
}
